package Sevlet;

import bean.SignUp;
import bean.TourRoute;
import bean.TourShift;
import bean.person;
import util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StaffManage extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = new String(req.getParameter("type").getBytes("ISO-8859-1"),"UTF-8");
        String handle = new String(req.getParameter("handle").getBytes("ISO-8859-1"),"UTF-8");
        //type 个人信息info、旅游线路route、班次shift、游客信息tourist、游客报名信息signup、
        //handle   增add、删del、改update、查search

        if (type.equals("info")){       //个人信息修改(不包括修改密码)
            String id = new String(req.getParameter("id").getBytes("ISO-8859-1"),"UTF-8");
            String userName = new String(req.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8");
            String name = new String(req.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
            String sex = new String(req.getParameter("sex").getBytes("ISO-8859-1"),"UTF-8");
            String birth = new String(req.getParameter("birth").getBytes("ISO-8859-1"),"UTF-8");
            String nation = new String(req.getParameter("nation").getBytes("ISO-8859-1"),"UTF-8");
            String phone = new String(req.getParameter("phone").getBytes("ISO-8859-1"),"UTF-8");
            String place = new String(req.getParameter("place").getBytes("ISO-8859-1"),"UTF-8");
            StaffDao sdao = new StaffDao();
            try {
                sdao.updateStaff(userName, id, name, sex, birth, nation, phone, place);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                person p = sdao.get(userName);
                req.getSession().setAttribute("person", p);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            resp.sendRedirect("http://localhost:8080/staff/info.jsp");

        }else if (type.equals("route")){
            TourRouteDao trdao = new TourRouteDao();
            TourRoute tr = new TourRoute();
            if (handle.equals("add")){
                String routeNo = null;
                try {
                    routeNo = trdao.updateRouteNo();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                String startPoint = new String(req.getParameter("startPoint").getBytes("ISO-8859-1"),"UTF-8");
                String endPoint = new String(req.getParameter("endPoint").getBytes("ISO-8859-1"),"UTF-8");
                int days = Integer.parseInt(req.getParameter("days"));
                String attraction = new String(req.getParameter("attraction").getBytes("ISO-8859-1"),"UTF-8");
                tr.setRouteNo(routeNo);
                tr.setStartPoint(startPoint);
                tr.setEndPoint(endPoint);
                tr.setDays(days);
                tr.setAttraction(attraction);
                try {
                    trdao.addTourRoute(tr);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }else if (handle.equals("del")){
                String routeNo = new String(req.getParameter("routeNo").getBytes("ISO-8859-1"),"UTF-8");
                try {
                    trdao.delTourRoute(routeNo);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }else if (handle.equals("update")){
                String routeNo = new String(req.getParameter("routeNo").getBytes("ISO-8859-1"),"UTF-8");
                String startPoint = new String(req.getParameter("startPoint").getBytes("ISO-8859-1"),"UTF-8");
                String endPoint = new String(req.getParameter("endPoint").getBytes("ISO-8859-1"),"UTF-8");
                int days = Integer.parseInt(req.getParameter("days"));
                String attraction = new String(req.getParameter("attraction").getBytes("ISO-8859-1"),"UTF-8");
                try {
                    trdao.updateTourRoute(routeNo, startPoint, endPoint, days, attraction);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }else {   //handle.equals("search")
                String find = new String(req.getParameter("find").getBytes("ISO-8859-1"),"UTF-8");
                List<TourRoute> TRList = new ArrayList<TourRoute>();
                try {
                    TRList = trdao.search(find);
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                for(TourRoute tr1 : TRList){
                    System.out.println(tr1.getRouteNo()+"-"+tr1.getStartPoint()+"-"+tr1.getEndPoint()+"-"+tr1.getDays()+"-"+tr1.getAttraction());
                }
                req.getSession().setAttribute("TRList",TRList);
            }
            resp.sendRedirect("http://localhost:8080/staff/setRoute.jsp");
        }else if (type.equals("shift")){
            TourShiftDao tsdao = new TourShiftDao();
            TourShift ts = new TourShift();
            if (handle.equals("add")){
                String shiftNo = null;
                try {
                    shiftNo = tsdao.updateShiftNo();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                String departureDate = new String(req.getParameter("departureDate").getBytes("ISO-8859-1"),"UTF-8");
                String tourismStandard = new String(req.getParameter("tourismStandard").getBytes("ISO-8859-1"),"UTF-8");
                int quote = Integer.parseInt(req.getParameter("quote"));
                float discountRate = Float.parseFloat(req.getParameter("discountRate"));
                String routeNo = new String(req.getParameter("routeNo").getBytes("ISO-8859-1"),"UTF-8");
                System.out.println(shiftNo+departureDate+tourismStandard+quote+discountRate+routeNo);
                ts.setShiftNo(shiftNo);
                ts.setDepartureDate(departureDate);
                ts.setTourismStandard(tourismStandard);
                ts.setQuote(quote);
                ts.setDiscountRate(discountRate);
                ts.setRouteNo(routeNo);
                try {
                    tsdao.addTourShift(ts);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }else if (handle.equals("del")){
                String shiftNo = new String(req.getParameter("shiftNo").getBytes("ISO-8859-1"),"UTF-8");
                try {
                    tsdao.delTourShift(shiftNo);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }else if (handle.equals("update")){
                String shiftNo = new String(req.getParameter("shiftNo").getBytes("ISO-8859-1"),"UTF-8");
                String departureDate = new String(req.getParameter("departureDate").getBytes("ISO-8859-1"),"UTF-8");
                String tourismStandard = new String(req.getParameter("tourismStandard").getBytes("ISO-8859-1"),"UTF-8");
                int quote = Integer.parseInt(req.getParameter("quote"));
                float discountRate = Float.parseFloat(req.getParameter("discountRate"));
                String routeNo = new String(req.getParameter("routeNo").getBytes("ISO-8859-1"),"UTF-8");
                try {
                    tsdao.updateTourShift(shiftNo, departureDate, tourismStandard, quote, discountRate);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }else {   //handle.equals("search")
                String find = new String(req.getParameter("find").getBytes("ISO-8859-1"),"UTF-8");
                List<TourShift> TSList = new ArrayList<TourShift>();
                try {
                    TSList = tsdao.search(find);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                req.getSession().setAttribute("TSList",TSList);
            }
            resp.sendRedirect("http://localhost:8080/staff/setShift.jsp");
        }else if (type.equals("tourist")){
            TouristDao tdao = new TouristDao();
            person tourist = new person();
            if (handle.equals("add")){
                String id = new String(req.getParameter("id").getBytes("ISO-8859-1"),"UTF-8");
                String userName = new String(req.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8");
                String password = new String(req.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
                String name = new String(req.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
                String sex = new String(req.getParameter("sex").getBytes("ISO-8859-1"),"UTF-8");
                String birth = new String(req.getParameter("birth").getBytes("ISO-8859-1"),"UTF-8");
                String nation = new String(req.getParameter("nation").getBytes("ISO-8859-1"),"UTF-8");
                String phone = new String(req.getParameter("phone").getBytes("ISO-8859-1"),"UTF-8");
                String place = new String(req.getParameter("place").getBytes("ISO-8859-1"),"UTF-8");
                tourist.setId(id);
                tourist.setUserName(userName);
                tourist.setPassword(password);
                tourist.setName(name);
                tourist.setSex(sex);
                tourist.setBirth(birth);
                tourist.setNation(nation);
                tourist.setPhone(phone);
                tourist.setPlace(place);
                try {
                    tdao.addTourist(tourist);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }else if (handle.equals("del")){
                String userName = new String(req.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8");
                try {
                    tdao.delTourist(userName);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                System.out.println(userName);
            }else if (handle.equals("update")){
                String id = new String(req.getParameter("id").getBytes("ISO-8859-1"),"UTF-8");
                String userName = new String(req.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8");
                String password = new String(req.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
                String name = new String(req.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
                String sex = new String(req.getParameter("sex").getBytes("ISO-8859-1"),"UTF-8");
                String birth = new String(req.getParameter("birth").getBytes("ISO-8859-1"),"UTF-8");
                String nation = new String(req.getParameter("nation").getBytes("ISO-8859-1"),"UTF-8");
                String phone = new String(req.getParameter("phone").getBytes("ISO-8859-1"),"UTF-8");
                String place = new String(req.getParameter("place").getBytes("ISO-8859-1"),"UTF-8");
                try {
                    tdao.updateTourist(userName,password, id, name, sex, birth, nation, phone, place);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }else {   //handle.equals("search")
                String find = new String(req.getParameter("find").getBytes("ISO-8859-1"),"UTF-8");
                List<person> TList = new ArrayList<person>();
                try {
                    TList = tdao.search(find);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                req.getSession().setAttribute("TList",TList);
            }
            resp.sendRedirect("http://localhost:8080/staff/updateTourist.jsp");
        }else if (type.equals("signup")){
            SignUpDao sgupdao = new SignUpDao();
            TouristDao tdao = new TouristDao();
            TourRouteDao trdao = new TourRouteDao();
            TourShiftDao tsdao = new TourShiftDao();
            SignUp sgup = new SignUp();
            person tourist = new person();
            TourRoute tr = new TourRoute();
            TourShift ts = new TourShift();

            if (handle.equals("add")){
                System.out.println("jin");
                String signUpNo = null;
                try {
                    signUpNo = sgupdao.updateSignUpNo();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                String userName = new String(req.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8");
                String routeNo = new String(req.getParameter("routeNo").getBytes("ISO-8859-1"),"UTF-8");
                String shiftNo = new String(req.getParameter("shiftNo").getBytes("ISO-8859-1"),"UTF-8");
                System.out.println(signUpNo+userName+routeNo+shiftNo);
                try {
                    tourist = tdao.get(userName);
                    tr = trdao.get(routeNo);
                    ts = tsdao.get(shiftNo);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                sgup.setSignUpNo(signUpNo);
                sgup.setUserName(tourist.getUserName());
                System.out.println(tourist.getUserName());
                sgup.setName(tourist.getName());
                sgup.setId(tourist.getId());
                sgup.setPhone(tourist.getPhone());
                sgup.setRouteNo(tr.getRouteNo());
                sgup.setStartPoint(tr.getStartPoint());
                sgup.setEndPoint(tr.getEndPoint());
                sgup.setDays(tr.getDays());
                sgup.setAttraction(tr.getAttraction());
                sgup.setShiftNo(ts.getShiftNo());
                sgup.setDepartureDate(ts.getDepartureDate());
                sgup.setTourismStandard(ts.getTourismStandard());
                sgup.setQuote(ts.getQuote());
                sgup.setDiscountRate(ts.getDiscountRate());
                float cost = ts.getQuote()*ts.getDiscountRate();
                sgup.setCost(cost);
                try {
                    sgupdao.addSignUp(sgup);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }else if (handle.equals("del")){
                String signUpNo = new String(req.getParameter("signUpNo").getBytes("ISO-8859-1"),"UTF-8");
                try {
                    sgupdao.delSignUp(signUpNo);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }else if (handle.equals("update")){     //signup的修改有限制
                String signUpNo = new String(req.getParameter("signUpNo").getBytes("ISO-8859-1"),"UTF-8");
                String phone = new String(req.getParameter("phone").getBytes("ISO-8859-1"),"UTF-8");
                String routeNo = new String(req.getParameter("routeNo").getBytes("ISO-8859-1"),"UTF-8");
                String shiftNo = new String(req.getParameter("shiftNo").getBytes("ISO-8859-1"),"UTF-8");
                try {
                    sgupdao.updateSignUp(signUpNo, phone, routeNo, shiftNo);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }else {   //handle.equals("get")
                String find = new String(req.getParameter("find").getBytes("ISO-8859-1"),"UTF-8");
                List<SignUp> SignupList = new ArrayList<SignUp>();
                try {
                    SignupList = sgupdao.search(find);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                req.getSession().setAttribute("SignupList",SignupList);
            }
            resp.sendRedirect("http://localhost:8080/staff/updateSignup.jsp");
        }else { }

    }
}
